const UserInfo = require("../models/UserInfo");
const { encrypt } = require("./index");
const { v4: uuidv4 } = require('uuid');
const DEFAULT_USER = {
  id:uuidv4(),  
  name:"超级管理员",
  user: "admin",
  passwd: encrypt("Cobot_123"), //ezhqxoPwsi5qE1fLgzZBuw== 注意：实际应用中密码应该被加密
  level:1,
  allow_sn:JSON.stringify({"patient_sn":"","doctor_sn":""})
};
function syncAdmin() {
  try {
    console.log("Default user",DEFAULT_USER);
    UserInfo.findOne({ where: { user: DEFAULT_USER.user } })
      .then((user) => {
        if (user) {
          // 用户存在，检查密码
          if (user.passwd !== DEFAULT_USER.passwd) {
            // 密码不一致，更新密码
            console.log("Default user already exists with diff password");
            // return user.update({ passwd: DEFAULT_USER.passwd });
          } else {
            // 密码一致，不做任何操作
            console.log("Default user already exists with same password");
          }
        } else {
          // 用户不存在，创建用户
          return UserInfo.create(DEFAULT_USER);
        }
      })
      .then((result) => {
        if (result) {
          console.log("Default user created or updated");
        }
      })
      .catch((err) => {
        console.error("Error handling default user:", err);
      });
  } catch (error) {
    console.log("syncAdmin error", error);
  }
}
module.exports = {
  syncAdmin,
};
